package exp5;

//---------------------------------------------------------------
//  SEnc.java   Modifier: 唐才铭   Date：2018-06-13
//
//  用于实验五的3,4,5的客户端；
//  对后缀表达式进行加密。
//---------------------------------------------------------------
import javax.crypto.Cipher;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.security.Key;

public class SEnc{
    public static String encrypt(String exp) throws Exception{
        String s = exp;
        String encryption = "";
        FileInputStream f=new FileInputStream("key1.dat");
        ObjectInputStream b=new ObjectInputStream(f);
        Key k=(Key)b.readObject( );
        Cipher cp=Cipher.getInstance("DESede");
        cp.init(Cipher.ENCRYPT_MODE, k);
        byte ptext[]=s.getBytes("UTF8");
        for(int i=0;i<ptext.length;i++){
            encryption += ptext[i]+",";
        }
        System.out.println("");
        byte ctext[]=cp.doFinal(ptext);
        for(int i=0;i<ctext.length;i++){
            System.out.print(ctext[i] +",");
        }
        FileOutputStream f2=new FileOutputStream("SEnc.dat");
        f2.write(ctext);
        return encryption;
    }
}